package com.rpinfo.editalog.venda;

import com.rpinfo.editalog.usuario.*;
import com.rpinfo.editalog.usuario.UsuarioDao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author camilo
 */
public class FinDaoHibernate {

  private Session session;
  private Transaction transacao;

  public void setSession(Session session) {
    this.session = session;
  }

  public void salvar(Fin obj) {
    this.transacao = this.session.beginTransaction();
    this.session.saveOrUpdate(obj);
    this.transacao.commit();
    this.session.close();
  }

  public void excluir(Fin obj) {
    this.transacao = this.session.beginTransaction();
    this.session.delete(obj);
    this.transacao.commit();
    this.session.close();
  }

  public Fin carregar(int id) {
    return (Fin) this.session.get(Fin.class, id);
  }

  public List<Fin> listar() {
    return this.session.createCriteria(Fin.class)
            .addOrder(Order.asc("cupom")).list();
  }

  public Usuario carregarNome(String nome) {
    Criteria criteria = this.session.createCriteria(Usuario.class);
    criteria.add(Restrictions.naturalId().set(Usuario.ATTRIBUTE_NOME, nome));
    return (Usuario) criteria.uniqueResult();
  }
}
